﻿//////////#include <iostream>
//////////using namespace std;
//////////class Video
//////////{
//////////private:
//////////	int type_, rent_, state_;
//////////	string name_;
//////////public:
//////////	Video(int type, string name, int rent, int state) :type_(type), name_(name), rent_(rent), state_(state) {};
//////////	void print()
//////////	{
//////////		if (type_ == 1)
//////////		{
//////////			cout << "黑胶片[" << name_ << "]";
//////////		}
//////////		else if (type_ == 2)
//////////		{
//////////			cout << "CD[" << name_ << "]";
//////////		}
//////////		else if (type_ == 3)
//////////		{
//////////			cout << "VCD[" << name_ << "]";
//////////		}
//////////		else 
//////////		{
//////////			cout << "DVD[" << name_ << "]";
//////////		}
//////////		if (state_ == 1)
//////////		{
//////////			cout << "已出租";
//////////		}
//////////		else if (state_ ==0)
//////////		{
//////////			cout << "未出租";
//////////		}
//////////	}
//////////};
//////////int main()
//////////{
//////////	int n,type,rent,state,check;
//////////	string name;
//////////	cin >> n;
//////////	for (int i = 0; i < n; i++)
//////////	{
//////////		cin >> type >> name >> rent >> state;
//////////		cin >> check;
//////////		Video video(type, name, rent, state);
//////////		if (check == 0)
//////////		{
//////////			video.print();
//////////			cout << endl;
//////////		}
//////////		else
//////////		{
//////////			video.print();
//////////			cout << endl;
//////////			if (state == 0)
//////////			{
//////////				cout << "未产生租金" << endl;
//////////			}
//////////			else
//////////			{
//////////				int rent1 = rent * check;
//////////				cout << "当前租金为" << rent1 << endl;
//////////			}
//////////		}
//////////	}
//////////	return 0;
//////////}
////////#include <iostream>
////////using namespace std;
////////class Node2D
////////{
////////protected:
////////	string place_;
////////public:
////////	Node2D(string place) :place_(place) {};
////////};
////////class Body :virtual public Node2D
////////{
////////protected:
////////	int maxHealth, health, defense;
////////public:
////////	Body(string place, int health_, int defense_) :Node2D(place), maxHealth(health_), defense(defense_)
////////	{
////////		health = maxHealth - defense;
////////	}
////////};
////////class Weapon :virtual public Node2D
////////{
////////protected:
////////	string w_name;
////////	int damage;
////////public:
////////	Weapon(string place, string wname, int damage_) :Node2D(place), w_name(wname), damage(damage_) { };
////////};
////////class Player :public Body, public Weapon
////////{
////////protected:
////////	string name;
////////public:
////////	int attack1;
////////	Player(string place, int health_, int defense_, string wname, int damage_, string name_) :Node2D(place), Body(place, health_, defense_), Weapon(place, wname, damage_), name(name_) {};
////////	void attack(Player& c)
////////	{
////////		health = maxHealth;
////////		c.health = c.maxHealth;
////////		while (health > 0 || c.health > 0)
////////		{
////////			cout << name << " deal " << damage - c.defense << " damage to " << c.name << endl;
////////			c.health = c.health - (damage - c.defense);
////////			if (c.health < 0||(c.health==0))
////////			{
////////				cout << name << " defeated " << c.name << " by " << w_name << " in " << place_ << endl;
////////				break;
////////			}
////////			else
////////			{
////////				cout << c.name << " still have " << c.health << " health" << endl;
////////			}
////////			cout << endl;
////////			cout << c.name << " deal " << c.damage - defense << " damage to " << name << endl;
////////			health =health - (c.damage - defense);
////////			if (health < 0||(health==0))
////////			{
////////				cout << c.name << " defeated " << name << " by " << c.w_name << " in " << place_ << endl;
////////				break;
////////			}
////////			else
////////			{
////////				cout << name << " still have " << health << " health" << endl;
////////			}
////////			cout << endl;
////////		}
////////	}
////////};
////////	int main()
////////	{
////////		string place, name1, wname1, name2, wname2;
////////		int health1, defense1, damage1, health2, defense2, damage2;
////////		cin >> place >> name1  >> health1>>defense1 >> wname1 >> damage1;
////////		cin >> name2  >> health2>>defense2 >> wname2 >> damage2;
////////		Player p1(place, health1, defense1, wname1, damage1, name1);
////////		Player p2(place, health2, defense2, wname2, damage2, name2);
////////		p1.attack(p2);
////////		return 0;
////////	}
//////#include <iostream>
//////#include <iomanip>
//////using namespace std;
//////class CDate
//////{
//////private:
//////	int year, month, day;
//////public:
//////	CDate();
//////	CDate(int y, int m, int d) :year(y), month(m), day(d) {}
//////	int gety()
//////	{
//////		return year;
//////	}
//////	int getm()
//////	{
//////		return month;
//////	}
//////	int getd()
//////	{
//////		return day;
//////	}
//////};
//////class Pet 
//////{
//////protected:
//////	string name;
//////	float length;
//////	float weight;
//////	CDate current;
//////public:
//////	Pet(string name_, float l, float w, CDate current_) :name(name_), length(l), weight(w),current(current_) {};
//////	void set(string name_, float l, float w, CDate current_)
//////	{
//////		name = name_;
//////		length = l;
//////		weight = w;
//////		current = current_;
//////	}
//////	virtual void display(CDate day) = 0;
//////};
//////class Cat :public Pet
//////{
//////public:
//////	Cat(string name_, float l, float w,  CDate current_) :Pet(name_, l, w, current_) {};
//////	int month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//////	virtual void display(CDate day)
//////	{
//////		if (day.gety() > current.gety() || day.gety() == current.gety() && day.getm() > current.getm() || day.gety() == current.gety() && day.getm() == current.getm() && day.getd() > current.getd())
//////		{
//////			cout << "error" << endl;
//////		}
//////		else
//////		{
//////			if (day.gety() % 400 == 0 || day.gety() % 4 == 0 && day.gety() % 100 != 0)
//////			{
//////				month[2] = 29;
//////			}
//////			if (current.gety() % 400 == 0 || current.gety() % 4 == 0 && current.gety() % 100 != 0)
//////			{
//////				month[2] = 29;
//////			}
//////			int sum = 0, sum1 = 0, sum2 = 0;
//////			for (int i = 1; i < current.gety(); i++)
//////			{
//////				if (current.gety() % 400 == 0 || current.gety() % 4 == 0 && current.gety() % 100 != 0)
//////				{
//////					sum = sum + 366;
//////				}
//////				else
//////				{
//////					sum = sum + 365;
//////				}
//////			}
//////			for (int i = 1; i < current.getm(); i++)
//////			{
//////				sum = sum +  month[i];
//////			}
//////				sum = sum + current.getd();
//////			for (int i = 1; i < day.gety(); i++)
//////			{
//////				if (day.gety() % 400 == 0 || day.gety() % 4 == 0 && day.gety() % 100 != 0)
//////				{
//////					sum = sum + 366;
//////				}
//////				else
//////				{
//////					sum1 = sum1 + 365;
//////				}
//////			}
//////			for (int i = 1; i <day.getm(); i++)
//////			{
//////				sum1 = sum1 + month[i];
//////			}
//////			sum1 = sum1 + day.getd();
//////			sum2 = sum - sum1;
//////			length = length +0.1 * sum2;
//////			weight = weight +0.2 * sum2;
//////			cout << name << " after " << sum2 << " day: " << "length=" << fixed << setprecision(2) << length << ",weight=" << weight << endl;
//////		}
//////	}
//////};
//////class Dog :public Pet
//////{
//////public:
//////	Dog(string name_, float l, float w, CDate current_) :Pet(name_, l, w, current_) {};
//////	int month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
//////	virtual void display(CDate day)
//////	{
//////		if (day.gety() > current.gety() || day.gety() == current.gety() && day.getm() > current.getm() || day.gety() == current.gety() && day.getm() == current.getm() && day.getd() > current.getd())
//////		{
//////			cout << "error" << endl;
//////		}
//////		else
//////		{
//////			if (day.gety() % 400 == 0 || day.gety() % 4 == 0 && day.gety() % 100 != 0)
//////			{
//////				month[2] = 29;
//////			}
//////			if (current.gety() % 400 == 0 || current.gety() % 4 == 0 && current.gety() % 100 != 0)
//////			{
//////				month[2] = 29;
//////			}
//////			int sum = 0, sum1 = 0, sum2 = 0;
//////			for (int i = 1; i < current.gety(); i++)
//////			{
//////				if (current.gety() % 400 == 0 || current.gety() % 4 == 0 && current.gety() % 100 != 0)
//////				{
//////					sum = sum + 366;
//////				}
//////				else
//////				{
//////					sum = sum + 365;
//////				}
//////			}
//////			for (int i = 1; i < current.getm(); i++)
//////			{
//////				sum = sum + month[i];
//////			}
//////			sum = sum + current.getd();
//////			for (int i = 1; i < day.gety(); i++)
//////			{
//////				if (day.gety() % 400 == 0 || day.gety() % 4 == 0 && day.gety() % 100 != 0)
//////				{
//////					sum = sum + 366;
//////				}
//////				else
//////				{
//////					sum1 = sum1 + 365;
//////				}
//////			}
//////			for (int i = 1; i < day.getm(); i++)
//////			{
//////				sum1 = sum1 + month[i];
//////			}
//////			sum1 = sum1 + day.getd();
//////			sum2 = sum- sum1;
//////			length = length + 0.2 * sum2;
//////			weight = weight + 0.1 * sum2;
//////			cout << name << " after " << sum2 << " day: " << "length=" <<fixed << setprecision(2) << length << ",weight=" << weight << endl;
//////		}
//////	}
//////};
//////int main()
//////{
//////	int t,y,m,d,type,y1,m1,d1;
//////	float l, w;
//////	string name;
//////	cin >> t >> y >> m >> d;
//////	CDate cdate1(y, m, d);
//////	Pet* p=NULL;
//////	for (int i = 0; i < t; i++)
//////	{
//////		cin >> type >> name>>l >> w >> y1 >> m1 >> d1;
//////		CDate cdate2(y1, m1, d1);
//////		if (type == 1)
//////		{
//////			p=new Cat(name, l, w, cdate2);
//////		}
//////		else
//////		{
//////			p = new Dog(name, l, w, cdate2);
//////		}
//////		p->display(cdate1);
//////	}
//////	return 0;
//////}
#include <iostream>
#include <iomanip>
using namespace std;
class CSet
{
private:
	int n_;
	int* data;
public:
	CSet() {};
	CSet(int n, int* p)
	{
		n_ = n;
		data = new int[n];
		for (int i = 0; i < n_; i++)
		{
			data[i] = p[i];
		}
		
	}
	CSet(const CSet& other) {
		n_ = other.n_;
		data = new int[n_];
		for (int i = 0; i < n_; i++) {
			data[i] = other.data[i];
		}
	}
	~CSet() 
	{
		delete[] data;
	}
	friend ostream& operator<<(ostream& c, const CSet& p);
	CSet operator+(const CSet& p)
	{
		CSet cset2;
		int n = n_ + p.n_;
		cset2.n_ = 0;
		cset2.data = new int[n];
		for (int i = 0; i < n_; i++)
		{
			cset2.data[cset2.n_++] = data[i];
		}
		for (int j = 0; j < p.n_; j++)
		{
			int flag = 1;
			for (int i = 0; i < n_; i++)
			{
				if (p.data[j] == data[i])
				{
					flag = 0;
					break;
				}
			}
			if (flag !=0)
			{
				cset2.data[cset2.n_++] = p.data[j];
			}
		}
		return cset2;
	}
	CSet operator-(CSet& p)
	{
		CSet cset2;
		cset2.data = new int[n_];
		cset2.n_ = 0;
		for (int j = 0; j < n_; j++)
		{
			int flag = 1;
			for (int i = 0; i < p.n_; i++)
			{
				if (data[j] == p.data[i])
				{
					flag = 0;
					break;
				}
			}
			if (flag != 0)
			{
				cset2.data[cset2.n_++] = data[j];
			}
		}
		return cset2;
	}
	CSet operator*(CSet& p)
	{
		CSet cset2;
		int n = n_ + p.n_;
		cset2.data = new int[n];
		cset2.n_ = 0;
		for (int j = 0; j < n_; j++)
		{
			for (int i = 0; i < p.n_; i++)
			{
				if (data[j] == p.data[i])
				{
					cset2.data[cset2.n_++] = data[j];
					break;
				}
			}
		}
		return cset2;
	}
};
 ostream& operator<<(ostream& c, const CSet& p)
{
	 for (int i = 0; i < p.n_; i++)
	 {
		 if (i == p.n_ - 1)
		 {
			 c << p.data[i];
		 }
		 else
		 c << p.data[i]<<" ";
	 }
	 return c;
}
int main()
{
	int t,n1,n2;
	cin >> t;
	for (int j = 0; j < t; j++)
	{
		cin >> n1;
		int* p1 = new int[n1];
		for (int i = 0; i < n1; i++)
		{
			cin >> p1[i];
		}
		CSet cset1(n1, p1);
		cin >> n2;
		int* p2 = new int[n2];
		for (int i = 0; i < n2; i++)
		{
			cin >> p2[i];
		}
		CSet cset2(n2, p2);
		delete[]p1;
		delete[]p2;
		cout << "A:" << cset1 << endl;
		cout << "B:" << cset2 << endl;
		CSet cset3 = cset1 + cset2;
		cout << "A+B:" << cset3 << endl;
		CSet cset4 = cset1 * cset2;
		cout << "A*B:" << cset4 << endl;
		CSet cset5 = (cset1 - cset2) + (cset2 - cset1);
		cout <<"(A-B)+(B-A):"<< cset5 << endl << "\n";
	}
	return 0;
}
//#include <iostream>
//#include <iomanip>
//#include <string>
//using namespace std;
//class CBook
//{
//private:
//	string name_, author_, publish_;
//	double price_;
//public:
//	CBook() {};
//	void set(string name, string author, double price, string publish)
//	{
//		name_ = name;
//		author_ = author;
//		price_A = price;
//		publish_ = publish;
//	}
//	friend int find(CBook* book, int n, int& max1index, int& max2index);
//};
//int find(CBook* book, int n, int& max1index, int& max2index)
//{
//	for (int i = 0; i < n; i++)
//	{
//		for (int j = i + 1; j < n; j++)
//		{
//			if (book[i].price_ < book[j].price_)
//			{
//				CBook temp = book[i];
//				book[i] = book[j];
//				book[j] = temp;
//			}
//		}
//	}
//	max1index = 0;
//	max2index = 1;
//	cout << book[max1index].name_ << endl << book[max1index].author_ << endl <<fixed << setprecision(2) << book[max1index].price_ << endl << book[max1index].publish_ <<endl<<"\n";
//	cout << book[max2index].name_ << endl << book[max2index].author_ << endl << fixed << setprecision(2) << book[max2index].price_ << endl << book[max2index].publish_ <<endl;
//	return 0;
//}
//int main()
//{
//	int t,n;
//	string name, author,publish;
//	double price;
//	cin >> t;
//	for (int i = 0; i < t; i++)
//	{
//		cin >> n;
//		CBook* p = new CBook[n];
//		for (int i = 0; i < n; i++)
//		{
//			getline(cin, name, ',');
//			getline(cin, author, ',');
//			cin >> price;
//			getchar();
//			getline(cin, publish);
//			p[i].set(name, author, price, publish);
//		}
//		int max1 = 0, max2 = 0;
//		find(p, n, max1, max2);
//		delete[]p;
//	}
//	
//	return 0;
//}